package com.wenlin.myproduction.util;

import static android.view.animation.Animation.INFINITE;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.wenlin.myproduction.MyApplication;
import com.wenlin.myproduction.R;

import java.util.List;

public class AnimUtils {
    private static AnimUtils sInstance;

    private AnimUtils() {

    }

    public static AnimUtils getInstance() {
        if (sInstance == null) {
            synchronized (AnimUtils.class) {
                if (sInstance == null) {
                    sInstance = new AnimUtils();
                }
            }
        }
        return sInstance;
    }

    /**
     * 返回每个按钮应该出现的角度(弧度单位)
     *
     * @param index
     * @return double 角度(弧度单位)
     */
    public static double getAngle(int total, int index) {
        switch (index) {//右边是0度
            case 0:
                return Math.toRadians(100);
            case 1:
                return Math.toRadians(150);
            case 2:
                return Math.toRadians(210);
            case 3:
                return Math.toRadians(-100);
        }
        return Math.toRadians(90 / (total - 1) * index + 90);
    }

    /***旋转*/
    public void rotateView(View view) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "rotation", 0, 359);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(-1);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(1500);
        animatorSet.playTogether(anim);
        animatorSet.start();

    }

    /***放大缩小*/
    public void scaleView(View view) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "scaleX", 1, 0.9f, 1, 1.1f, 1);
        ObjectAnimator animY = ObjectAnimator.ofFloat(view, "scaleY", 1, 0.9f, 1, 1.1f, 1);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.playTogether(anim, animY);
        animatorSet.start();

    }

    /***渐显*/
    public void alphaShow(View view) {
        if (view == null) return;
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
        alphaAnimation.setDuration(600);
        alphaAnimation.setFillAfter(true);
        view.startAnimation(alphaAnimation);
    }

    /***渐隐*/
    public void alphaDismiss(View view) {
        if (view == null) return;
        AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
        alphaAnimation.setDuration(300);
        alphaAnimation.setFillAfter(true);
        view.startAnimation(alphaAnimation);
    }

    /***放大缩小*/
    public void scale(View view, CommonCallback callback, boolean isBig) {
        view.setEnabled(false);
        float toX;
        toX = isBig ? 2 : 1.2f;
        ScaleAnimation mAnimation = new ScaleAnimation(1f, toX, 1f, toX, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mAnimation.setDuration(200);
        mAnimation.setRepeatCount(0);
        mAnimation.setRepeatMode(Animation.RESTART);
        mAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (callback != null) {
                    callback.callback(true);
                    view.setEnabled(true);
                    mAnimation.cancel();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        view.startAnimation(mAnimation);
    }

    /***放大缩小*/
    public void scaleSmall(View view, CommonCallback callback, boolean isBig) {
        view.setEnabled(false);
        float toX;
        toX = isBig ? 1.5f : 1.1f;
        ScaleAnimation mAnimation = new ScaleAnimation(1f, toX, 1f, toX, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mAnimation.setDuration(200);
        mAnimation.setRepeatCount(0);
        mAnimation.setRepeatMode(Animation.RESTART);
        mAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (callback != null) {
                    callback.callback(true);
                    view.setEnabled(true);
                    mAnimation.cancel();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        view.startAnimation(mAnimation);
    }

    /***先放大再缩小，最终恢复成原始大小*/
    public void scaleToBigAndRestore(View view) {
        ScaleAnimation mAnimation = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mAnimation.setDuration(800);
        int repeatCount = 2;//20230612 重复次数：动画先放大再恢复原始大小，算一次
        mAnimation.setRepeatCount(2 * repeatCount - 1);//20230612 因为RepeatMode=Animation.REVERSE，即逆向模式，所以需要奇数次的重复次数，加上初始的一次，总共是偶数次，才能最后恢复控件原始的模样，避免出现控件大小突变的情况
        mAnimation.setRepeatMode(Animation.REVERSE);//20230612 固定的
        if (view != null) view.startAnimation(mAnimation);
    }

    /***先放大再缩小，最终恢复成原始大小*/
    public void scaleToBigAndRestoreForLevelUpNumberView(View view) {
        if (view == null) return;
        ValueAnimator mAnimation = ValueAnimator.ofFloat(1f, 1.5f, 0.7f, 1.1f, 1f);
        mAnimation.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            view.setScaleX(value);
            view.setScaleY(value);
        });
        mAnimation.setDuration(800);
        mAnimation.setInterpolator(new LinearInterpolator());
        mAnimation.start();
    }

    /***先放大再缩小，最终恢复成原始大小*/
    public void scaleToBigAndRestoreForTimes(View view, int repeatCount) {
        ScaleAnimation mAnimation = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mAnimation.setDuration(800);
        mAnimation.setRepeatCount(2 * repeatCount - 1);//20230612 因为RepeatMode=Animation.REVERSE，即逆向模式，所以需要奇数次的重复次数，加上初始的一次，总共是偶数次，才能最后恢复控件原始的模样，避免出现控件大小突变的情况
        mAnimation.setRepeatMode(Animation.REVERSE);//20230612 固定的
        if (view != null) view.startAnimation(mAnimation);
    }

    /***先放大再缩小，最终恢复成原始大小*/
    public void heartBeatForTimes(View view) {
        //放大和缩小
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, 1.175f), Keyframe.ofFloat(.2f, 1.3f), Keyframe.ofFloat(.3f, 1.3f), Keyframe.ofFloat(.4f, 1.25f), Keyframe.ofFloat(.5f, 1.2f), Keyframe.ofFloat(.6f, 1.15f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.05f), Keyframe.ofFloat(.9f, 1f), Keyframe.ofFloat(1f, 1f));
        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, 1.175f), Keyframe.ofFloat(.2f, 1.3f), Keyframe.ofFloat(.3f, 1.3f), Keyframe.ofFloat(.4f, 1.25f), Keyframe.ofFloat(.5f, 1.2f), Keyframe.ofFloat(.6f, 1.15f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.05f), Keyframe.ofFloat(.9f, 1f), Keyframe.ofFloat(1f, 1f));
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY).setDuration(800);
        animator.setRepeatCount(INFINITE);//20230612 因为RepeatMode=Animation.REVERSE，即逆向模式，所以需要奇数次的重复次数，加上初始的一次，总共是偶数次，才能最后恢复控件原始的模样，避免出现控件大小突变的情况
        animator.start();
    }

    /**
     * 为点赞按钮设置点赞动画并播放
     *
     * @param view 点赞按钮
     * @param
     * @return 所求
     */
    public void startLikeAnim(View view) {
        float shakeFactor = 2f;//抖动程度一般为2就行了
        //放大和缩小
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, 1.f), Keyframe.ofFloat(.2f, 1.05f), Keyframe.ofFloat(.3f, 1.1f), Keyframe.ofFloat(.4f, 1.15f), Keyframe.ofFloat(.5f, 1.2f), Keyframe.ofFloat(.6f, 1.15f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.05f), Keyframe.ofFloat(.9f, 1f), Keyframe.ofFloat(1f, 1f));
        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, 1.f), Keyframe.ofFloat(.2f, 1.05f), Keyframe.ofFloat(.3f, 1.1f), Keyframe.ofFloat(.4f, 1.15f), Keyframe.ofFloat(.5f, 1.2f), Keyframe.ofFloat(.6f, 1.15f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.05f), Keyframe.ofFloat(.9f, 1f), Keyframe.ofFloat(1f, 1f));
        //旋转
        PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION, Keyframe.ofFloat(0f, 0f), Keyframe.ofFloat(.1f, -3f * shakeFactor), Keyframe.ofFloat(.2f, -3f * shakeFactor), Keyframe.ofFloat(.3f, 3f * shakeFactor), Keyframe.ofFloat(.4f, -3f * shakeFactor), Keyframe.ofFloat(.5f, 3f * shakeFactor), Keyframe.ofFloat(.6f, -3f * shakeFactor), Keyframe.ofFloat(.7f, 3f * shakeFactor), Keyframe.ofFloat(.8f, -3f * shakeFactor), Keyframe.ofFloat(.9f, 3f * shakeFactor), Keyframe.ofFloat(1f, 0));
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate).setDuration(1000);
        animator.setRepeatCount(0);
        animator.start();
    }

    /**
     * 左右摇动+放大缩小的动画，引起用户注意
     *
     * @param view        控件
     * @param repeatTimes 重复次数
     */
    public void tada(View view, int repeatTimes) {
        if (view == null) return;
        float shakeFactor = 1.2f;
        //放大和缩小
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, .9f),
                Keyframe.ofFloat(.2f, .9f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.1f),
                Keyframe.ofFloat(.5f, 1.1f),
                Keyframe.ofFloat(.6f, 1.1f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.1f),
                Keyframe.ofFloat(.9f, 1.1f),
                Keyframe.ofFloat(1f, 1f));
        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, .9f),
                Keyframe.ofFloat(.2f, .9f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.1f),
                Keyframe.ofFloat(.5f, 1.1f),
                Keyframe.ofFloat(.6f, 1.1f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.1f),
                Keyframe.ofFloat(.9f, 1.1f),
                Keyframe.ofFloat(1f, 1f));
        //旋转
        PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(.1f, -3f * shakeFactor),
                Keyframe.ofFloat(.2f, -3f * shakeFactor),
                Keyframe.ofFloat(.3f, 3f * shakeFactor),
                Keyframe.ofFloat(.4f, -3f * shakeFactor),
                Keyframe.ofFloat(.5f, 3f * shakeFactor),
                Keyframe.ofFloat(.6f, -3f * shakeFactor),
                Keyframe.ofFloat(.7f, 3f * shakeFactor),
                Keyframe.ofFloat(.8f, -3f * shakeFactor),
                Keyframe.ofFloat(.9f, 3f * shakeFactor),
                Keyframe.ofFloat(1f, 0));
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate);
        animator.setDuration(1500);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatCount(repeatTimes > 0 ? repeatTimes : INFINITE);
        animator.start();
    }

    /**
     * 左右摇动+放大缩小的动画，引起用户注意
     *
     * @param view 控件
     */
    public void tada(View view) {
        tada(view, 0);
    }

    /**
     * 左右摇动+渐显的动画
     *
     * @param view        控件
     * @param shakeFactor 抖动程度
     */
    public void rotate(View view, float shakeFactor) {
        // 旋转
        PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(.17f, 12f * shakeFactor),
                Keyframe.ofFloat(.25f, 12f * shakeFactor),
                Keyframe.ofFloat(.39f, -7f * shakeFactor),
                Keyframe.ofFloat(.45f, -7f * shakeFactor),
                Keyframe.ofFloat(.61f, 5f * shakeFactor),
                Keyframe.ofFloat(.65f, 5f * shakeFactor),
                Keyframe.ofFloat(.83f, -3.5f * shakeFactor),
                Keyframe.ofFloat(.85f, -3.5f * shakeFactor),
                Keyframe.ofFloat(1f, 0));
        //图的宽度是88*110
        view.setPivotX(52);
        view.setPivotY(9);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhRotate);
        animator.setDuration(3000);
        animator.start();
    }

    /**
     * 按钮选中动画
     *
     * @param view 控件
     */
    public void btnOnAnim(View view) {
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator imgScaleUpYAnim = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.2f);
        imgScaleUpYAnim.setDuration(100);
        ObjectAnimator imgScaleUpXAnim = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.2f);
        imgScaleUpXAnim.setDuration(100);
//        AlphaAnimation alphaAnimation = new AlphaAnimation(0.9f, 1f);
//        alphaAnimation.setDuration(100);
//        alphaAnimation.setFillAfter(true);
        animatorSet.playTogether(imgScaleUpYAnim, imgScaleUpXAnim);
        animatorSet.start();
//        v.startAnimation(alphaAnimation);
    }

    /**
     * 按钮取消选中动画
     *
     * @param view 控件
     */
    public void btnOffAnim(View view) {
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator imgScaleDownYAnim = ObjectAnimator.ofFloat(view, "scaleY", 1.2f, 1f);
        imgScaleDownYAnim.setDuration(100);
        ObjectAnimator imgScaleDownXAnim = ObjectAnimator.ofFloat(view, "scaleX", 1.2f, 1f);
        imgScaleDownXAnim.setDuration(100);
//        AlphaAnimation alphaAnimation2 = new AlphaAnimation(1f, 0.9f);
//        alphaAnimation2.setDuration(100);
//        alphaAnimation2.setFillAfter(true);
        animatorSet.playTogether(imgScaleDownXAnim, imgScaleDownYAnim);
        animatorSet.start();
//        v.startAnimation(alphaAnimation2);
    }

    /**
     * 返回左右抖动+放大缩小的动画
     *
     * @param view        控件
     * @param shakeFactor 抖动的程度
     * @return 所求
     */
    public ObjectAnimator like(View view, float shakeFactor) {
        //放大和缩小
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, 1.f),
                Keyframe.ofFloat(.2f, 1.05f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.15f),
                Keyframe.ofFloat(.5f, 1.2f),
                Keyframe.ofFloat(.6f, 1.15f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.05f),
                Keyframe.ofFloat(.9f, 1f),
                Keyframe.ofFloat(1f, 1f));
        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, 1.f),
                Keyframe.ofFloat(.2f, 1.05f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.15f),
                Keyframe.ofFloat(.5f, 1.2f),
                Keyframe.ofFloat(.6f, 1.15f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.05f),
                Keyframe.ofFloat(.9f, 1f),
                Keyframe.ofFloat(1f, 1f));
        //旋转
        PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(.1f, -3f * shakeFactor),
                Keyframe.ofFloat(.2f, -3f * shakeFactor),
                Keyframe.ofFloat(.3f, 3f * shakeFactor),
                Keyframe.ofFloat(.4f, -3f * shakeFactor),
                Keyframe.ofFloat(.5f, 3f * shakeFactor),
                Keyframe.ofFloat(.6f, -3f * shakeFactor),
                Keyframe.ofFloat(.7f, 3f * shakeFactor),
                Keyframe.ofFloat(.8f, -3f * shakeFactor),
                Keyframe.ofFloat(.9f, 3f * shakeFactor),
                Keyframe.ofFloat(1f, 0));
        return ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate).setDuration(1000);
    }

    public void transToLeft(View view) {
        TranslateAnimation transRight = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, -1f,
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f);
        transRight.setDuration(1200);
        transRight.setFillAfter(true);
        view.startAnimation(transRight);
    }

    public void transFromLeftToCenter(View view) {
        int width = ScreenUtils.getCurrentScreenWidth(MyApplication.getMyApplicationContext());
        int vWidth = view.getWidth();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(-vWidth, 0);
        valueAnimator.addUpdateListener(animation -> {
            float valueFactor = (float) animation.getAnimatedValue();
            view.setTranslationX(valueFactor);
        });
        valueAnimator.setInterpolator(new OvershootInterpolator());
        valueAnimator.setDuration(500);
        valueAnimator.start();
    }

    public void transToOtherSide(View left, View right) {
        TranslateAnimation transLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0.3f,
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f);
        transLeft.setDuration(2000);
        transLeft.setFillAfter(true);
        TranslateAnimation transRight = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, -0.3f,
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f);
        transRight.setDuration(2000);
        transRight.setFillAfter(true);
        left.startAnimation(transLeft);
        right.startAnimation(transRight);
    }

    /**
     * 按钮上下浮动动画
     *
     * @param view 控件
     */
    public void upDown(View view) {
        PropertyValuesHolder pv = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_Y,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(1f, 6f));
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pv);
        animator.setDuration(2000);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatCount(INFINITE);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.start();
    }

    /**
     * 按钮左右浮动动画
     *
     * @param view 控件
     */
    public void leftRight(View view) {
        PropertyValuesHolder pv = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(1f, 50f));
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pv);
        animator.setDuration(5000);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatCount(INFINITE);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.start();
    }

    /**
     * 按钮左右浮动动画
     *
     * @param view 控件
     */
    public void leftRightSmall(View view) {
        PropertyValuesHolder pv = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(1f, 25f));
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pv);
        animator.setDuration(4000);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatCount(INFINITE);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.start();
    }

    /**
     * 水平翻转
     *
     * @param view      target
     * @param duration  time
     * @param direction 只能传1或-1，1为从左开始翻转，-1位从右开始翻转
     * @return 动画集合
     */
    public AnimatorSet flip(View view, int duration, int direction) {
        AnimatorSet animSet = new AnimatorSet();
        if (view != null) {
            if (direction != 1 && direction != -1) direction = 1;
            view.setCameraDistance(16000 * view.getResources().getDisplayMetrics().density);
            ObjectAnimator rotationY = new ObjectAnimator();
            rotationY.setDuration(duration).setPropertyName("rotationY");
            rotationY.setFloatValues(0, -90 * direction);
            ObjectAnimator _rotationY = new ObjectAnimator();
            _rotationY.setDuration(duration).setPropertyName("rotationY");
            _rotationY.setFloatValues(90 * direction, 0);
            _rotationY.setStartDelay(duration);
            ObjectAnimator scale = new ObjectAnimator();
            scale.setDuration(duration).setPropertyName("scaleY");
            scale.setFloatValues(1, 0.99f);
            ObjectAnimator _scale = new ObjectAnimator();
            _scale.setDuration(duration).setPropertyName("scaleY");
            _scale.setFloatValues(0.99f, 1);
            _scale.setStartDelay(duration);
            animSet.setTarget(view);
            rotationY.setTarget(view);
            _rotationY.setTarget(view);
            scale.setTarget(view);
            _scale.setTarget(view);
            animSet.playTogether(rotationY, _rotationY, scale, _scale);
            animSet.start();
        }
        return animSet;
    }
}
